package com.zuipin.interview.baiyapeng.web;

import com.zuipin.interview.baiyapeng.parse.exception.ParseException;
import com.zuipin.interview.baiyapeng.util.Pair;
import com.zuipin.interview.baiyapeng.web.response.ResponseResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import static com.zuipin.interview.baiyapeng.parse.TimeParser.parseTime;
import static com.zuipin.interview.baiyapeng.parse.TimeParser.toStr;

@RestController
public class CalculateController {

    @GetMapping(value = "calcuate")
    public ResponseResult<?> calculate(@RequestParam(value = "tp1") String tp1, @RequestParam(value = "tp2") String tp2) {
        ResponseResult<Map<String, Object>> result = new ResponseResult<>();
        try {
            Map<String, Object> data = new HashMap<>();
            Pair<BigDecimal> left = parseTime(tp1);
            Pair<BigDecimal> right = parseTime(tp2);
            if (left.getR().compareTo(right.getL()) < 0 || left.getL().compareTo(right.getR()) > 0) {
                data.put("leftRemain", new String[]{tp1});
                data.put("intersection", null);
                data.put("rightRemain", new String[]{tp2});
            } else {
                BigDecimal intersection_left = left.getL().compareTo(right.getL()) < 0 ? right.getL() : left.getL();
                BigDecimal intersection_right = left.getR().compareTo(right.getR()) > 0 ? right.getR() : left.getR();
                data.put("intersection", toStr(intersection_left, intersection_right));
                if (left.getL().compareTo(intersection_left) < 0 && left.getR().compareTo(intersection_right) > 0) {
                    data.put("leftRemain", new String[]{toStr(left.getL(), intersection_left), toStr(intersection_right, left.getR())});
                    data.put("rightRemain", new String[]{});
                } else if (right.getL().compareTo(intersection_left) < 0 && right.getR().compareTo(intersection_right) > 0) {
                    data.put("leftRemain", new String[]{});
                    data.put("rightRemain", new String[]{toStr(right.getL(), intersection_left), toStr(intersection_right, right.getR())});
                } else {
                    if (left.getL().compareTo(right.getL()) < 0) {
                        data.put("leftRemain", new String[]{toStr(left.getL(), intersection_left)});
                        data.put("rightRemain", new String[]{toStr(intersection_right, right.getR())});
                    } else {
                        data.put("leftRemain", new String[]{toStr(intersection_right, left.getR())});
                        data.put("rightRemain", new String[]{toStr(right.getL(), intersection_left)});
                    }
                }
            }
            result.setData(data);
        } catch (ParseException e) {
            System.err.println(e.getMessage());
            result.setError_code(50001);
            result.setError_message(e.getMessage());
        }
        return result;
    }
}
